// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.gameStates;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Locale;
import se.krka.kahlua.vm.KahluaTable;
import zombie.Lua.LuaManager;
import zombie.core.Core;
import zombie.core.logger.ExceptionLogger;
import zombie.core.skinnedmodel.ModelManager;
import zombie.debug.DebugLog;
import zombie.debug.DebugOptions;
import zombie.input.GameKeyboard;
import zombie.scripting.ScriptManager;
import zombie.scripting.ScriptParser;
import zombie.scripting.objects.ModelAttachment;
import zombie.scripting.objects.ModelScript;
import zombie.ui.UIElement;
import zombie.ui.UIManager;
import zombie.vehicles.EditVehicleState;

public final class AttachmentEditorState extends GameState {
    public static AttachmentEditorState instance;
    private EditVehicleState.LuaEnvironment m_luaEnv;
    private boolean bExit = false;
    private final ArrayList<UIElement> m_gameUI = new ArrayList();
    private final ArrayList<UIElement> m_selfUI = new ArrayList();
    private boolean m_bSuspendUI;
    private KahluaTable m_table = null;

    @Override
    public void enter() {
        instance = this;
        if (this.m_luaEnv == null) {
            this.m_luaEnv = new EditVehicleState.LuaEnvironment(LuaManager.platform, LuaManager.converterManager, LuaManager.env);
        }

        this.saveGameUI();
        if (this.m_selfUI.size() == 0) {
            this.m_luaEnv.caller.pcall(this.m_luaEnv.thread, this.m_luaEnv.env.rawget("AttachmentEditorState_InitUI"), new Object[0]);
            if (this.m_table != null && this.m_table.getMetatable() != null) {
                this.m_table.getMetatable().rawset("_LUA_RELOADED_CHECK", Boolean.FALSE);
            }
        } else {
            UIManager.UI.addAll(this.m_selfUI);
            this.m_luaEnv.caller.pcall(this.m_luaEnv.thread, this.m_table.rawget("showUI"), this.m_table);
        }

        this.bExit = false;
    }

    @Override
    public void yield() {
        this.restoreGameUI();
    }

    @Override
    public void reenter() {
        this.saveGameUI();
    }

    @Override
    public void exit() {
        this.restoreGameUI();
    }

    @Override
    public void render() {
        byte _byte = 0;
        Core.getInstance().StartFrame(_byte, true);
        this.renderScene();
        Core.getInstance().EndFrame(_byte);
        Core.getInstance().RenderOffScreenBuffer();
        if (Core.getInstance().StartFrameUI()) {
            this.renderUI();
        }

        Core.getInstance().EndFrameUI();
    }

    @Override
    public GameStateMachine.StateAction update() {
        if (!this.bExit && !GameKeyboard.isKeyPressed(65)) {
            this.updateScene();
            return GameStateMachine.StateAction.Remain;
        } else {
            return GameStateMachine.StateAction.Continue;
        }
    }

    public static AttachmentEditorState checkInstance() {
        if (instance != null) {
            if (instance.m_table != null && instance.m_table.getMetatable() != null) {
                if (instance.m_table.getMetatable().rawget("_LUA_RELOADED_CHECK") == null) {
                    instance = null;
                }
            } else {
                instance = null;
            }
        }

        return instance == null ? new AttachmentEditorState() : instance;
    }

    private void saveGameUI() {
        this.m_gameUI.clear();
        this.m_gameUI.addAll(UIManager.UI);
        UIManager.UI.clear();
        this.m_bSuspendUI = UIManager.bSuspend;
        UIManager.bSuspend = false;
        UIManager.setShowPausedMessage(false);
        UIManager.defaultthread = this.m_luaEnv.thread;
    }

    private void restoreGameUI() {
        this.m_selfUI.clear();
        this.m_selfUI.addAll(UIManager.UI);
        UIManager.UI.clear();
        UIManager.UI.addAll(this.m_gameUI);
        UIManager.bSuspend = this.m_bSuspendUI;
        UIManager.setShowPausedMessage(true);
        UIManager.defaultthread = LuaManager.thread;
    }

    private void updateScene() {
        ModelManager.instance.update();
        if (GameKeyboard.isKeyPressed(17)) {
            DebugOptions.instance.ModelRenderWireframe.setValue(!DebugOptions.instance.ModelRenderWireframe.getValue());
        }
    }

    private void renderScene() {
    }

    private void renderUI() {
        UIManager.render();
    }

    public void setTable(KahluaTable kahluaTable) {
        this.m_table = kahluaTable;
    }

    public Object fromLua0(String func) {
        byte _byte = -1;
        switch (func.hashCode()) {
            case 3127582:
                if (func.equals("exit")) {
                    _byte = 0;
                }
            default:
                switch (_byte) {
                    case 0:
                        this.bExit = true;
                        return null;
                    default:
                        throw new IllegalArgumentException("unhandled \"" + func + "\"");
                }
        }
    }

    public Object fromLua1(String func, Object arg0) {
        byte _byte = -1;
        switch (func.hashCode()) {
            case 1396535690:
                if (func.equals("writeScript")) {
                    _byte = 0;
                }
            default:
                switch (_byte) {
                    case 0:
                        ModelScript modelScript = ScriptManager.instance.getModelScript((String)arg0);
                        if (modelScript == null) {
                            throw new NullPointerException("model script \"" + arg0 + "\" not found");
                        }

                        ArrayList arrayList = this.readScript(modelScript.getFileName());
                        if (arrayList != null) {
                            this.updateScript(modelScript.getFileName(), arrayList, modelScript);
                        }

                        return null;
                    default:
                        throw new IllegalArgumentException(String.format("unhandled \"%s\" \"%s\"", func, arg0));
                }
        }
    }

    private ArrayList<String> readScript(String string0) {
        StringBuilder stringBuilder = new StringBuilder();
        string0 = zombie.ZomboidFileSystem.instance.getString(string0);
        File file = new File(string0);

        try {
            FileReader fileReader = new FileReader(file);

            try {
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                try {
                    String string1 = System.lineSeparator();

                    String string2;
                    while ((string2 = bufferedReader.readLine()) != null) {
                        stringBuilder.append(string2);
                        stringBuilder.append(string1);
                    }
                } catch (Throwable throwable0) {
                    try {
                        bufferedReader.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }

                    throw throwable0;
                }

                bufferedReader.close();
            } catch (Throwable throwable2) {
                try {
                    fileReader.close();
                } catch (Throwable throwable3) {
                    throwable2.addSuppressed(throwable3);
                }

                throw throwable2;
            }

            fileReader.close();
        } catch (Throwable throwable4) {
            ExceptionLogger.logException(throwable4);
            return null;
        }

        String string3 = ScriptParser.stripComments(stringBuilder.toString());
        return ScriptParser.parseTokens(string3);
    }

    private void updateScript(String string0, ArrayList<String> arrayList0, ModelScript modelScript) {
        string0 = zombie.ZomboidFileSystem.instance.getString(string0);

        for (int int0 = arrayList0.size() - 1; int0 >= 0; int0--) {
            String string1 = ((String)arrayList0.get(int0)).trim();
            int int1 = string1.indexOf("{");
            int int2 = string1.lastIndexOf("}");
            String string2 = string1.substring(0, int1);
            if (string2.startsWith("module")) {
                string2 = string1.substring(0, int1).trim();
                String[] string3 = string2.split("\\s+");
                String string4 = string3.length > 1 ? string3[1].trim() : "";
                if (string4.equals(modelScript.getModule().getName())) {
                    String string5 = string1.substring(int1 + 1, int2).trim();
                    ArrayList arrayList1 = ScriptParser.parseTokens(string5);

                    for (int int3 = arrayList1.size() - 1; int3 >= 0; int3--) {
                        String string6 = ((String)arrayList1.get(int3)).trim();
                        if (string6.startsWith("model")) {
                            int1 = string6.indexOf("{");
                            string2 = string6.substring(0, int1).trim();
                            string3 = string2.split("\\s+");
                            String string7 = string3.length > 1 ? string3[1].trim() : "";
                            if (string7.equals(modelScript.getName())) {
                                string6 = this.modelScriptToText(modelScript, string6).trim();
                                arrayList1.set(int3, string6);
                                String string8 = System.lineSeparator();
                                String string9 = String.join(string8 + "\t", arrayList1);
                                string9 = "module " + string4 + string8 + "{" + string8 + "\t" + string9 + string8 + "}" + string8;
                                arrayList0.set(int0, string9);
                                this.writeScript(string0, arrayList0);
                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    private String modelScriptToText(ModelScript modelScript, String string0) {
        ScriptParser.Block block0 = ScriptParser.parse(string0);
        block0 = (ScriptParser.Block)block0.children.get(0);

        for (int int0 = block0.children.size() - 1; int0 >= 0; int0--) {
            ScriptParser.Block block1 = (ScriptParser.Block)block0.children.get(int0);
            if ("attachment".equals(block1.type)) {
                block0.elements.remove(block1);
                block0.children.remove(int0);
            }
        }

        for (int int1 = 0; int1 < modelScript.getAttachmentCount(); int1++) {
            ModelAttachment modelAttachment = modelScript.getAttachment(int1);
            ScriptParser.Block block2 = block0.getBlock("attachment", modelAttachment.getId());
            if (block2 == null) {
                block2 = new ScriptParser.Block();
                block2.type = "attachment";
                block2.id = modelAttachment.getId();
                block2.setValue(
                    "offset",
                    String.format(
                        Locale.US, "%.4f %.4f %.4f", modelAttachment.getOffset().x(), modelAttachment.getOffset().y(), modelAttachment.getOffset().z()
                    )
                );
                block2.setValue(
                    "rotate",
                    String.format(
                        Locale.US, "%.4f %.4f %.4f", modelAttachment.getRotate().x(), modelAttachment.getRotate().y(), modelAttachment.getRotate().z()
                    )
                );
                if (modelAttachment.getBone() != null) {
                    block2.setValue("bone", modelAttachment.getBone());
                }

                block0.elements.add(block2);
                block0.children.add(block2);
            } else {
                block2.setValue(
                    "offset",
                    String.format(
                        Locale.US, "%.4f %.4f %.4f", modelAttachment.getOffset().x(), modelAttachment.getOffset().y(), modelAttachment.getOffset().z()
                    )
                );
                block2.setValue(
                    "rotate",
                    String.format(
                        Locale.US, "%.4f %.4f %.4f", modelAttachment.getRotate().x(), modelAttachment.getRotate().y(), modelAttachment.getRotate().z()
                    )
                );
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        String string1 = System.lineSeparator();
        block0.prettyPrint(1, stringBuilder, string1);
        return stringBuilder.toString();
    }

    private void writeScript(String string1, ArrayList<String> arrayList) {
        String string0 = zombie.ZomboidFileSystem.instance.getString(string1);
        File file = new File(string0);

        try {
            FileWriter fileWriter = new FileWriter(file);

            try {
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

                try {
                    DebugLog.General.printf("writing %s\n", string1);

                    for (String string2 : arrayList) {
                        bufferedWriter.write(string2);
                    }

                    this.m_luaEnv.caller.pcall(this.m_luaEnv.thread, this.m_table.rawget("wroteScript"), new Object[]{this.m_table, string0});
                } catch (Throwable throwable0) {
                    try {
                        bufferedWriter.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }

                    throw throwable0;
                }

                bufferedWriter.close();
            } catch (Throwable throwable2) {
                try {
                    fileWriter.close();
                } catch (Throwable throwable3) {
                    throwable2.addSuppressed(throwable3);
                }

                throw throwable2;
            }

            fileWriter.close();
        } catch (Throwable throwable4) {
            ExceptionLogger.logException(throwable4);
        }
    }
}
